/// <summary>
// CodeFx.MySqlProviders - .NET Providers for MySql using the framework CodeFx
// Laurent Morisseau
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// Laurent Morisseau
/// </summary>
/// <author><a href="mailto:laurent.morisseau@gmail.com">Laurent Morisseau</a></author>


namespace CodeFx.MySqlProviders
{
    using System;
    using System.Text;
    using System.Web;
    using System.Data;
    using System.ComponentModel;
    using MySql.Data.MySqlClient;
    using System.Collections;
    using CodeFx;
    using System.Web.Profile;
    using CodeFx.Web;

    internal static partial class Aspnet_profileDTO
	{

        internal static object Delete_InactiveProfiles(uint ApplicationId, int ProfileAuthOptions, DateTime InactiveSinceDate)
        {

            MySqlConnection conn = ConnectionString.Current.OpenConnection;       
            MySqlCommand cmd = new MySqlCommand("DELETE FROM aspnet_profile WHERE UserId IN (SELECT UserId FROM aspnet_users u WHERE ApplicationId = ?ApplicationId AND (LastActivityDate <= ?InactiveSinceDate) AND ( (?ProfileAuthOptions = 2) OR (?ProfileAuthOptions = 0 AND IsAnonymous = 1) OR (?ProfileAuthOptions = 1 AND IsAnonymous = 0) ) )", conn);
            cmd.Parameters.Add(new MySqlParameter("?ApplicationId", ApplicationId));
            cmd.Parameters.Add(new MySqlParameter("?ProfileAuthOptions", ProfileAuthOptions));
            cmd.Parameters.Add(new MySqlParameter("?InactiveSinceDate", InactiveSinceDate));
            try
            {
                return cmd.ExecuteScalar();
            }
            finally
            {
                conn.Close();
            }
        }

        internal static object Count_InactiveProfiles(uint ApplicationId, int ProfileAuthOptions, DateTime InactiveSinceDate)
        {

            MySqlConnection conn = ConnectionString.Current.OpenConnection;       
            MySqlCommand cmd = new MySqlCommand("SELECT  COUNT(*) FROM    aspnet_users u, aspnet_profile p WHERE   ApplicationId = ?ApplicationId AND u.UserId = p.UserId AND (LastActivityDate <= ?InactiveSinceDate) AND ((?ProfileAuthOptions = 2) OR (?ProfileAuthOptions = 0 AND IsAnonymous = 1) OR (?ProfileAuthOptions = 1 AND IsAnonymous = 0))", conn);
            cmd.Parameters.Add(new MySqlParameter("?ApplicationId", ApplicationId));
            cmd.Parameters.Add(new MySqlParameter("?ProfileAuthOptions", ProfileAuthOptions));
            cmd.Parameters.Add(new MySqlParameter("?InactiveSinceDate", InactiveSinceDate));

            try
            {
                return cmd.ExecuteScalar();
            }
            finally
            {
                conn.Close();
            }
        }

        internal static ProfileInfoCollection GetProfiles(MySqlParameter[] args, uint ApplicationId, int ProfileAuthOptions, long lbound, long ubound)
        {
            ProfileInfoCollection collection2;
            MySqlDataReader dr = null;

            MySqlConnection conn = ConnectionString.Current.OpenConnection; 
            MySqlCommand cmd = new MySqlCommand("SELECT  u.UserName, u.IsAnonymous, u.LastActivityDate, p.LastUpdatedDate, LENGTH(p.PropertyNames) + LENGTH(p.PropertyValuesString) + LENGTH(p.PropertyValuesBinary) FROM aspnet_users u, aspnet_profile p WHERE   ApplicationId = ?ApplicationId AND u.UserId = p.UserId AND (?InactiveSinceDate IS NULL OR LastActivityDate <= ?InactiveSinceDate) AND ((?ProfileAuthOptions = 2) OR (?ProfileAuthOptions = 0 AND IsAnonymous = 1) OR (?ProfileAuthOptions = 1 AND IsAnonymous = 0) ) AND (?UserNameToMatch IS NULL OR UserName LIKE ?UserNameToMatch) ORDER BY UserName limit ?lbound, ?ubound", conn);
            cmd.Parameters.Add(new MySqlParameter("?ApplicationId", ApplicationId));
            cmd.Parameters.Add(new MySqlParameter("?ProfileAuthOptions", ProfileAuthOptions));
            cmd.Parameters.Add(new MySqlParameter("?lbound", lbound));
            cmd.Parameters.Add(new MySqlParameter("?ubound", ubound));
            bool blnInactiveSinceDate = false;
            bool blnUserNameToMatch = false;

            foreach (MySqlParameter parameter1 in args)
            {
                if (string.Equals(parameter1.ParameterName, "?InactiveSinceDate"))
                    blnInactiveSinceDate = true;
                if (string.Equals(parameter1.ParameterName, "?UserNameToMatch"))
                    blnUserNameToMatch = true;
                cmd.Parameters.Add(parameter1);
            }
            if (!blnInactiveSinceDate)
                cmd.Parameters.Add(new MySqlParameter("?InactiveSinceDate", null));
            if (!blnUserNameToMatch)
                cmd.Parameters.Add(new MySqlParameter("?UserNameToMatch", (string)null));
            try
            {
                dr = cmd.ExecuteReader(CommandBehavior.SequentialAccess);

                ProfileInfoCollection collection1 = new ProfileInfoCollection();
                while (dr.Read())
                {
                    string text1 = dr.GetString(0);
                    bool flag1 = dr.GetBoolean(1);
                    DateTime time1 = DateTime.SpecifyKind(dr.GetDateTime(2), DateTimeKind.Utc);
                    DateTime time2 = DateTime.SpecifyKind(dr.GetDateTime(3), DateTimeKind.Utc);
                    int num2 = dr.GetInt32(4);
                    collection1.Add(new ProfileInfo(text1, flag1, time1, time2, num2));
                }
                collection2 = collection1;

            }
            catch (Exception ex)
            {
                throw new DALException(cmd.CommandText, ex.InnerException);
            }
            finally
            {
                if (dr != null)
                {
                    dr.Close();
                }
                conn.Close();
            }
            return collection2;
        }

        internal static object Count_TotalProfiles(MySqlParameter[] args, uint ApplicationId, int ProfileAuthOptions)
        {

            MySqlConnection conn = ConnectionString.Current.OpenConnection; 
            MySqlCommand cmd = new MySqlCommand("SELECT  count(*) FROM aspnet_users u, aspnet_profile p WHERE   ApplicationId = ?ApplicationId AND u.UserId = p.UserId AND (?InactiveSinceDate IS NULL OR LastActivityDate <= ?InactiveSinceDate) AND ((?ProfileAuthOptions = 2) OR (?ProfileAuthOptions = 0 AND IsAnonymous = 1) OR (?ProfileAuthOptions = 1 AND IsAnonymous = 0) ) AND (?UserNameToMatch IS NULL OR UserName LIKE ?UserNameToMatch) ", conn);
            cmd.Parameters.Add(new MySqlParameter("?ApplicationId", ApplicationId));
            cmd.Parameters.Add(new MySqlParameter("?ProfileAuthOptions", ProfileAuthOptions));
            bool blnInactiveSinceDate = false;
            bool blnUserNameToMatch = false;
            
            foreach (MySqlParameter parameter1 in args)
            {
                if (string.Equals(parameter1.ParameterName, "?InactiveSinceDate"))
                    blnInactiveSinceDate = true;
                if (string.Equals(parameter1.ParameterName, "?UserNameToMatch"))
                    blnUserNameToMatch = true;
                cmd.Parameters.Add(parameter1);
            }
            if (!blnInactiveSinceDate)
                cmd.Parameters.Add(new MySqlParameter("?InactiveSinceDate", null));
            if (!blnUserNameToMatch)
                cmd.Parameters.Add(new MySqlParameter("?UserNameToMatch", (string)null));

            try
            {
                return cmd.ExecuteScalar();
            }
            finally
            {
                conn.Close();
            }
        }   
	}
}